package com.markyao.recos.regist;

import com.markyao.aop.annotations.WalEnabled;
import com.markyao.aop.annotations.WalParam;
import com.markyao.service.raft.BroadcastRegistService;
import com.markyao.service.raft.BroadcastService;
import com.markyao.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Slf4j(topic = "m.RecosRegistServiceImpl")
@Service
public class RecosRegistServiceImpl implements RecosRegistService {

    @Autowired
    @Qualifier("registWorker")
    RegistWorker registWorker;
    @Autowired
    RecosRegistConfig recosRegistConfig;
    @Autowired
    BroadcastRegistService broadcastService;

    @WalEnabled(value = "registAppName",log = "注册appName")
    @Override
    public Result registAppName(@WalParam String appName, HttpServletRequest request) {
        String remoteHost = request.getRemoteHost();
        registWorker.registAppName(appName,remoteHost);
        log.info("【recos】 {} 注册appName成功.",appName);
        broadcastService.registAppName(appName,request);
        return Result.success("【recos】 注册appName成功.");
    }

    @Override
    public Result registUrl(@WalParam String appName,@WalParam String url) {
        registWorker.registUrl(appName,url);
        log.info("【recos】 注册url成功. {} -> {}",appName,url);
        // TODO: 2023/8/30 心跳机制
        broadcastService.registUrl(appName,url);
        return Result.success("【recos】 注册url成功.");
    }

    @Override
    public Result registWeight(String appName, Integer weight) {
        registWorker.registWeight(appName,weight);
        log.info("【recos】 注册weight成功. {} -> {}",appName,weight);
        broadcastService.registWeight(appName,weight);
        return Result.success("【recos】 注册weight成功.");

    }

    @Override
    public Result registServer0(String appName, String interfaceNAME, String clazz) {
        registWorker.registServer00(appName,interfaceNAME,clazz);
        log.info("【recos】 注册server0成功. {} -> {}",appName,interfaceNAME);
        broadcastService.registServer0(appName,interfaceNAME,clazz);
        return Result.success("【recos】 注册server0成功.");
    }

    @Override
    public Result registRefrence(String appName, String refrenceName) {
        registWorker.registRefrence(appName,refrenceName);
        log.info("【recos】 注册refrence成功. {} -> {}",appName,refrenceName);
        broadcastService.registRefrence(appName,refrenceName);
        return Result.success("【recos】 注册refrence成功.");
    }

    @Override
    public Result registConnect(String appName, String targetUrl) {
        registWorker.registConnect(appName,targetUrl);
        log.info("【recos】 注册connect成功. {} -> {}",appName,targetUrl);
        broadcastService.registConnect(appName,targetUrl);
        return Result.success("【recos】 注册connect成功.");
    }


}
